package com.my.main.service.userAndRight.serviceImpl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.common.utils.IdWorker;
import com.my.main.entity.userAndRight.SysRightsRoleResource;
import com.my.main.mapper.userAndRight.SysRightsRoleResourceMapper;
import com.my.main.service.userAndRight.SysRightsRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class SysRightsRoleResourceServiceImpl extends BaseServiceImpl<SysRightsRoleResource>  implements SysRightsRoleResourceService {

    @Autowired
    private SysRightsRoleResourceMapper sysRightsRoleResourceMapper;
    @Resource(name= "sysRightsRoleResourceMapper")
    public void setBaseMapper(BaseMapper<SysRightsRoleResource> sysRightsRoleResourceMapper) {
        super.setBaseMapper(sysRightsRoleResourceMapper);
    }

    @Autowired
    public IdWorker idWorker;

    @Override
    public List<SysRightsRoleResource> querySysRightsResourceListForRole(Map map) {
        return sysRightsRoleResourceMapper.queryListByMap(map);
    }

    @Override
    public void isUpdateSysRightsRoleResource(String roleId, List<String> ridList, String ip, String userId) {
        try {

            // 删除所有
            sysRightsRoleResourceMapper.isDeleteByRoleId(roleId);

            // 添加
            for(String rid : ridList) {
                SysRightsRoleResource srrr = new SysRightsRoleResource();
                srrr.setRelationId(idWorker.nextId() + "");
                srrr.setRoleId(roleId);
                srrr.setResourceId(rid);
                srrr.setAddIp(ip);
                srrr.setAddUserId(userId);
                srrr.setLastEditBy(userId);
                srrr.setLastEditIp(ip);
                srrr.setIsDel(false);

                sysRightsRoleResourceMapper.isSave(srrr);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
